﻿using System;
using Medianamik.Core.Action;

namespace Medianamik.Core.Urls
{
    public class UrlChangedActionHandler : IActionHandler
    {
       public void Initialize(ActionManager events)
        {
            events.AfterCreateNodeProperty += AfterSaveNodeProperty;
            events.AfterUpdateNodeProperty += AfterSaveNodeProperty;
            events.AfterPublishNodeInstance += AfterPublishNodeInstance;
            events.AfterDeleteNodeInstance += AfterDeleteNodeInstance;
            events.AfterDeleteNode += AfterDeleteNode;
            events.AfterChangeNodeInstanceActiveStatus += AfterChangeNodeInstanceActiveStatus;
            events.AfterCreateNode += AfterSaveNode;
            events.AfterUpdateNode += AfterSaveNode;
        }

        void AfterSaveNode(ActionEventArgs<Node> pArgs)
        {
            //Si on modifie un noeud Website, on recharge le UrlDictionary
            if (pArgs.Item.NodeType.GetParentIds(true).Contains(MedianamikTypes.WebSite) ||
                pArgs.Item.NodeType.GetParentIds(true).Contains(MedianamikTypes.Domain))
            {
                new ObjectFactory<IUrlDictionary>().Get().Reload();
            }
        }
        
        void AfterPublishNodeInstance(ActionEventArgs<NodeInstance> obj)
        {
            if (obj.Item.HasUrl)
            {
                UpdateUrlsFor(obj.Item.Node.ID);
            }
        }

        void AfterDeleteNode(ActionEventArgs<Node> args)
        {
            if (args.Item.HasUrl)
            {
                new ObjectFactory<IUrlDictionary>().Get().Remove(args.Item.ID);
            }

            if (args.Item.NodeType.GetParentIds(true).Contains(MedianamikTypes.Domain))
            {
                new ObjectFactory<IUrlDictionary>().Get().Reload();
            }
        }

        void AfterDeleteNodeInstance(ActionEventArgs<NodeInstance> args)
        {
            if (args.Item.HasUrl)
            {
                new ObjectFactory<IUrlDictionary>().Get().Remove(args.Item.Node.ID, args.Item.Culture);
            }
        }

        void AfterSaveNodeProperty(NodePropertyActionEventArgs obj)
        {
            if (obj.Item.Name.Equals("Url", StringComparison.InvariantCultureIgnoreCase))
            {
                UpdateUrlsFor(obj.Item.Node.ID);
            }
        }

        private void UpdateUrlsFor(Guid nodeId)
        {
            var urlDictionary = new ObjectFactory<IUrlDictionary>().Get();
            urlDictionary.UpdateUrlsFor(nodeId);
        }

        void AfterChangeNodeInstanceActiveStatus(ActionEventArgs<NodeInstance> args)
        {
            if (args.Item.HasUrl
                && !string.IsNullOrEmpty(args.Item.Properties["Url"].GetValue<string>()))
            {
                if (args.Item.IsActive)
                {
                    UpdateUrlsFor(args.Item.Node.ID);
                }
                else
                {
                    new ObjectFactory<IUrlDictionary>().Get().Remove(args.Item.Node.ID, args.Item.Culture);
                }
            }
        }

        public string Description
        {
            get { return "Permet de synchroniser le UrlDictionary quand une URL a changé ou lorsque un Noeud est activé/désactivé"; }
        }
    }
}
